"""
Author        yongfa
Date          2022-07-14 13:43:18
LastEditTime  2023-07-08 10:38:54
LastEditors   yongfa
Description   路由
"""

from flask_restx import Resource

from app.extensions.decorators import admin_required, casbin_required
from app.extensions.utils.cache import cached
from .parameters import permission_params
from .schemas import permission_schemas
from .services import RoleService, MenuService, ApiService, routes_list_data
from .models import Role, Menu, Apis


default_output_fields = ['id', 'name', 'value', 'label', 'parent_id']


def configure_router(ns):

    for _, schema in permission_schemas.items():
        ns.add_model(schema.name, schema)

    # 角色
    @ns.route('/roles')
    @ns.doc(responses={404: "Todo not found"})
    class RoleListResource(Resource):
        method_decorators = [casbin_required]

        @ns.doc(parser=permission_params['get_list'], model=permission_schemas['RoleListSchema'])
        @cached()
        def get(self):
            """获取角色列表"""
            return RoleService(model=Role, args=permission_params['get_list']).get_list()

        @ns.doc(body=permission_params['role_params'], model=permission_schemas['RoleSchema'])
        def post(self):
            """创建角色详情"""
            return RoleService(model=Role, args=permission_params['role_params']).create()

    @ns.route('/reload_casbin')
    class RoleReloadResource(Resource):
        method_decorators = [casbin_required]

        def get(self):
            """重新加载角色-权限"""
            return RoleService(model=Role).reload_policy()

    @ns.route('/roles/tree')
    class RoleTreeResource(Resource):
        method_decorators = [casbin_required]

        @ns.doc(model=permission_schemas['RoleTreeSchema'])
        # @cached()
        def get(self):
            """获取角色树"""
            return RoleService(model=Role).get_tree(exclude_fields=['apis', 'menus', 'dicts'])

    @ns.route('/roles/copy')
    class RoleCopyResource(Resource):
        method_decorators = [casbin_required]

        @ns.doc(body=permission_params['role_copy_params'])
        # @cached()
        def post(self):
            """复制角色详情"""
            return RoleService(model=Role, args=permission_params['role_copy_params']).copy()

    @ns.route('/roles/<int:id>')
    @ns.doc(params={"id": "The Role ID"})
    class RoleResource(Resource):
        method_decorators = [casbin_required]

        @ns.doc(model=permission_schemas['RoleSchema'])
        def get(self, id):
            """获取角色详情"""
            return RoleService(model=Role).get_detail(id)

        @ns.doc(body=permission_params['role_update_params'], model=permission_schemas['RoleSchema'])
        def put(self, id):
            """修改角色详情"""
            return RoleService(model=Role, args=permission_params['role_update_params']).update(id)

        @ns.doc(model=permission_schemas['RoleSchema'])
        def delete(self, id):
            """删除角色详情"""
            return RoleService(model=Role).delete(id)

    # 接口
    @ns.route('/apis')
    class ApiListResource(Resource):
        method_decorators = [casbin_required]

        @ns.doc(parser=permission_params['get_list'], model=permission_schemas['ApiListSchema'])
        def get(self):
            """获取接口列表"""
            return ApiService(model=Apis, args=permission_params['get_list']).get_list()

        @ns.doc(body=permission_params['api_params'], model=permission_schemas['ApiSchema'])
        def post(self):
            """创建接口详情"""
            return ApiService(model=Apis, args=permission_params['api_params']).create()

    @ns.route("/apis/tree")
    class ApiTreeResource(Resource):
        method_decorators = [casbin_required]

        @ns.doc(model=permission_schemas['ApiTreeSchema'])
        def get(self):
            """获取接口树"""
            return ApiService(model=Apis).get_tree(output_fields=default_output_fields)

    @ns.route('/apis/<int:id>')
    @ns.doc(params={"id": "The Api ID"})
    class ApiResource(Resource):
        method_decorators = [casbin_required]

        @ns.doc(model=permission_schemas['ApiSchema'])
        def get(self, id):
            """获取接口详情"""
            return ApiService(model=Apis).get_detail(id)

        @ns.doc(body=permission_params['api_update_params'], model=permission_schemas['ApiSchema'])
        def put(self, id):
            """修改接口详情"""
            return ApiService(model=Apis, args=permission_params['api_update_params']).update(id)

        @ns.doc(model=permission_schemas['ApiSchema'])
        def delete(self, id):
            """删除接口详情"""
            return ApiService(model=Apis).delete(id)

    # 菜单
    @ns.route('/menus')
    class MenuListResource(Resource):
        method_decorators = [casbin_required]

        @ns.doc(parser=permission_params['get_list'], model=permission_schemas['MenuListSchema'])
        def get(self):
            """获取菜单列表"""
            return MenuService(model=Menu, args=permission_params['get_list']).get_list()

        @ns.doc(body=permission_params['menu_params'], model=permission_schemas['MenuSchema'])
        def post(self):
            """创建菜单详情"""
            return MenuService(model=Menu, args=permission_params['menu_params']).create()

    @ns.route('/menus/tree')
    class MenuTreeResource(Resource):
        method_decorators = [casbin_required]

        @ns.doc(model=permission_schemas['MenuTreeSchema'])
        def get(self):
            """获取菜单树"""
            return MenuService(model=Menu).get_tree(output_fields=default_output_fields)

    @ns.route('/menus/<int:id>')
    @ns.doc(params={"id": "The Menu ID"})
    class MenuResource(Resource):
        method_decorators = [casbin_required]

        @ns.doc(model=permission_schemas['MenuSchema'])
        def get(self, id):
            """获取菜单详情"""
            return MenuService(model=Menu).get_detail(id)

        @ns.doc(body=permission_params['menu_update_params'], model=permission_schemas['MenuSchema'])
        def put(self, id):
            """修改菜单详情"""
            return MenuService(model=Menu, args=permission_params['menu_update_params']).update(id)

        @ns.doc(model=permission_schemas['MenuSchema'])
        def delete(self, id):
            """删除菜单详情"""
            return MenuService(model=Menu).delete(id)

    # 路由
    @ns.route('/routes')
    class RouteListResource(Resource):
        method_decorators = [admin_required]

        def get(self):
            """获取路由列表"""
            return routes_list_data()
